<template>
    <div class="login-page">
        <div class="login-illustration left">
            <!-- 左侧插画 SVG（可替换为本地图片） -->
            <img src="@/assets/病人.png" alt="patient" />
        </div>
        <div class="login-center">
            <div class="login-card">
                <div class="login-title">登录</div>
                <div class="login-subtitle">七尾医疗服务后台管理系统</div>
                <el-form :model="form" :rules="rules" ref="formRef" label-width="0" class="login-form">
                    <el-form-item prop="Name">
                        <el-input v-model="form.Name" placeholder="请输入您的账号" clearable />
                    </el-form-item>
                    <el-form-item prop="Pwd">
                        <el-input v-model="form.Pwd" type="password" placeholder="请输入密码" show-password clearable />
                    </el-form-item>
                    <el-form-item>
                        <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
                    </el-form-item>
                    <div class="login-tip">忘记密码请联系管理员</div>
                    <el-form-item>
                        <el-button type="primary" class="login-btn" @click="onLogin" :loading="loading">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="login-illustration right">
            <!-- 右侧插画 SVG（可替换为本地图片） -->
            <img src="@/assets/医生.png" alt="doctor" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { axiosr } from '@/http/axiosRead'

const formRef = ref<FormInstance>()
const form = reactive({
    Name: '',
    Pwd: ''
})
const rules = reactive<FormRules>({
    Name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    Pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})
const loading = ref(false)
const rememberMe = ref(false)

onMounted(() => {
    const name = localStorage.getItem('rememberName')
    const pwd = localStorage.getItem('rememberPwd')
    if (name && pwd) {
        form.Name = name
        form.Pwd = pwd
        rememberMe.value = true
    }
})

function onLogin() {
    if (!formRef.value) return
    formRef.value.validate((valid) => {
        if (!valid) return
        loading.value = true
        axiosr.get('/Account/Login', {
            params: {
                Name: form.Name,
                Pwd: form.Pwd
            }
        }).then(res => {
            loading.value = false
            if (res.data.code === 200) {
                ElMessage.success('登录成功')
                
                localStorage.setItem("Uid", res.data.data.accMsg.id)
                localStorage.setItem("name", res.data.data.accMsg.name)
                localStorage.setItem("RoleId", res.data.data.accMsg.roleIds)
                localStorage.setItem("RoleName", res.data.data.accMsg.roleName)
                localStorage.setItem("token", res.data.data.toKen)
                // 记住密码逻辑
                if (rememberMe.value) {
                    localStorage.setItem('rememberName', form.Name)
                    localStorage.setItem('rememberPwd', form.Pwd)
                } else {
                    localStorage.removeItem('rememberName')
                    localStorage.removeItem('rememberPwd')
                }
                location.href = "/home"
            }
            else {
                ElMessage.error(res.data.msg || '登录失败')
            }
        }).catch(() => {
            loading.value = false
            ElMessage.error('网络错误')
        })
    })
}
</script>

<style scoped>
.login-page {
    min-height: 100vh;
    width: 100vw;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.login-illustration {
    flex: 1 1 0;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 260px;
    max-width: 340px;
    height: 100vh;
}

.login-illustration img {
    max-width: 220px;
    max-height: 60vh;
    width: 100%;
    height: auto;
    object-fit: contain;
    /* 让两侧图片高度接近 */
    display: block;
}

.login-illustration.left {
    justify-content: flex-end;
    padding-right: 32px;
}

.login-illustration.right {
    justify-content: flex-start;
    padding-left: 32px;
}

.login-center {
    flex: 0 0 420px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 340px;
    z-index: 2;
    margin: 0 24px;
}

.login-card {
    background: #0A7AFF;
    border-radius: 24px;
    box-shadow: 0 8px 32px rgba(10, 122, 255, 0.10);
    padding: 48px 40px 36px 40px;
    width: 100%;
    max-width: 420px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

.login-title {
    color: #fff;
    font-size: 2.4rem;
    font-weight: 900;
    text-align: center;
    margin-bottom: 0.5rem;
    letter-spacing: 2px;
}

.login-subtitle {
    color: #fff;
    font-size: 1.1rem;
    text-align: center;
    margin-bottom: 1.5rem;
    font-weight: 500;
    letter-spacing: 1px;
}

.login-form {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
}

.el-form-item {
    margin-bottom: 0;
}

.el-input__wrapper {
    border-radius: 12px !important;
    box-shadow: none !important;
    border: 1.5px solid #fff !important;
    background: #fff;
    min-height: 48px;
}

.el-input__inner::placeholder {
    color: #b0b8c9;
    opacity: 1;
}

.login-btn {
    width: 100%;
    font-size: 1.22rem;
    border-radius: 14px;
    height: 54px;
    background: #ff9f3a;
    border: none;
    font-weight: 800;
    letter-spacing: 1px;
    color: #fff;
    transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
}

.login-btn:hover {
    background: #ffb95e;
    transform: translateY(-2px) scale(1.02);
}

.login-tip {
    color: #fff;
    font-size: 0.98rem;
    text-align: right;
    margin-bottom: 0.5rem;
    width: 100%;
}

@media (max-width: 1100px) {
    .login-illustration {
        min-width: 120px;
        max-width: 160px;
    }

    .login-center {
        min-width: 220px;
        flex-basis: 220px;
        margin: 0 8px;
    }

    .login-card {
        padding: 28px 2vw 18px 2vw;
        max-width: 99vw;
    }

    .login-title {
        font-size: 1.5rem;
    }

    .login-btn {
        font-size: 1rem;
        height: 44px;
    }
}

@media (max-width: 800px) {
    .login-page {
        flex-direction: column;
        justify-content: flex-start;
        min-height: unset;
        height: auto;
    }

    .login-illustration {
        display: none;
    }

    .login-center {
        min-width: 90vw;
        flex-basis: unset;
        margin: 40px 0;
    }

    .login-card {
        padding: 28px 4vw 18px 4vw;
        max-width: 99vw;
    }

    .login-title {
        font-size: 1.5rem;
    }

    .login-btn {
        font-size: 1rem;
        height: 44px;
    }
}
</style>
